﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Trains.Models;
using Trains.Services;
using Trains.Services.Interface;

namespace Trains.Controllers
{
    public class HomeController : Controller
    {

        private IStationService _stationService;
        public HomeController(IStationService stationService)
        {
            _stationService = stationService;
        }

        public IActionResult Index(string result)
        {
            var model = new TrainViewModel();
            model.Result = result;
            return View(model);
        }

        public IActionResult SearchByRoute(TrainViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.Result = _stationService.GetDistanceByRoute(model.Route.ToUpper());
                return View("Index", model);
            }
            else
            {
                return View("Index", model);
            }
        }

        public IActionResult SearchNumberOfTrips(TrainViewModel model)
        {
            if (model.FilterValue == null)
            {
                ModelState.AddModelError("FilterValue", "The stops is required.");
            }
            if (string.IsNullOrEmpty(model.FromStationName) || string.IsNullOrEmpty(model.ToStationName))
            {
                ModelState.AddModelError("", "The stations are required.");
            }
            if (ModelState.IsValid)
            {                
                model.Result = _stationService.GetNumberOfTripsByFilter(model.FromStationName.ToUpper(), model.ToStationName.ToUpper(), model.OperationType, model.FilterValue.Value);                

                return RedirectToAction("Index", new { result = model.Result });
            }
            else
            {
                return View("Index", model);
            }
        }

        public IActionResult SearchShortestRoute(TrainViewModel model)
        {
            if (string.IsNullOrEmpty(model.FromStationName) || string.IsNullOrEmpty(model.ToStationName))
            {
                ModelState.AddModelError("", "The stations are required.");
            }
            if (ModelState.IsValid)
            {
                model.Result = _stationService.GetShortestRoute(model.FromStationName.ToUpper(), model.ToStationName.ToUpper());

                return RedirectToAction("Index", new { result = model.Result });
            }
            else
            {
                return View("Index", model);
            }
        }

        public IActionResult SearchNumberOfTripWithDistance(TrainViewModel model)
        {
            if (model.FilterValue == null)
            {
                ModelState.AddModelError("FilterValue", "The distance is required.");
            }
            if (string.IsNullOrEmpty(model.FromStationName) || string.IsNullOrEmpty(model.ToStationName))
            {
                ModelState.AddModelError("", "The stations are required.");
            }
            if (ModelState.IsValid)
            {
                model.Result = _stationService.GetNumberOfTripWithDistance(model.FromStationName.ToUpper(), model.ToStationName.ToUpper(), model.FilterValue.Value);

                return RedirectToAction("Index", new { result = model.Result });
            }
            else
            {
                return View("Index", model);
            }
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
